<!--
 * @Author: ch cwl_ch@163.com
 * @Date: 2022-12-19 17:45:07
 * @LastEditors: ch
 * @LastEditTime: 2022-12-30 15:30:32
 * @Description: file content
-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
  <title>-</title>
</head>
<script type="text/javascript" src="./uniwebview.js"></script>
<script type="text/javascript">

const identity = 1;
let routerQuery = getQuery();
let source = null;

if(window.EventSource){
    console.info("此浏览器支持SSE");
    source = new EventSource(`${routerQuery.uri}/connect?userId=${routerQuery.userId}&identity=${identity}`);

    // 监听服务的推送的消息
    source.addEventListener("message",function (e){
        let content = e.data;
        setMessageContent(content);
    });

}else {
    setMessageContent("此浏览器不支持");
}

function setMessageContent(content){
    uni.postMessage({
        data:  JSON.parse(content)
    });
}
/**
 * @Description: 获取url参数
 * @return {*}
 */
function getQuery (){
    let queryArr = window.location.search.substr(1).split('&');
    let obj = {};
    queryArr.forEach(item => {
        const arr = item.split('=');
        obj[arr[0]] = decodeURIComponent(arr[1]);
    })
    return obj;
}
// 暂未调用
function sourceClose(){
    console.info("close方法执行");
    // 客户端source的关闭
    source.close();
    // 服务端map的移除
    httpRequest = new XMLHttpRequest();
    httpRequest.open("get","http://localhost:9000/close?userId="+userId+"&identity="+identity);
    httpRequest.send();
}

</script>
</html>
